/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.modudf;

import java.io.File;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.modudf.common.XmlUtils;

public class Tomcat {

    private static int PORT = -1; // default port

    public static boolean startup() {
        int code = CMD.process("bat/tomcat-startup.bat");
        CMD.checkCode(code, "Tomcat");
        return (code == 0 || code == 1);
    }

    public static boolean run() {
        int code = CMD.process("bat/tomcat-run.bat");
        CMD.checkCode(code, "Tomcat");
        return (code == 0 || code == 1);
    }

    public static boolean shutdown() {
        int code = CMD.process("bat/tomcat-shutdown.bat");
        CMD.checkCode(code, "Tomcat");
        return (code == 0);
    }

    public static boolean stop() {
        int code = CMD.process("bat/tomcat-stop.bat");
        CMD.checkCode(code, "Tomcat");
        return (code == 0);
    }

    public static boolean serviceInstall() {
        int code = CMD.process("bat/tomcat-service-install.bat");
        CMD.checkCode(code, "Tomcat");
        return (code == 0);
    }

    public static boolean serviceRemove() {
        int code = CMD.process("bat/tomcat-service-remove.bat");
        CMD.checkCode(code, "Tomcat");
        return (code == 0);
    }

    public static boolean isRunning() {
        if (PORT == -1) {
            try {
                PORT = Integer.parseInt(findPort());
            } catch (NumberFormatException nfe) {
                Logger.getLogger(Tomcat.class.getName()).log(Level.SEVERE, null, nfe);
            }
        }
        try {
            Socket s = new Socket("localhost", PORT);
            return true;
        } catch (UnknownHostException uhe) {
            return false;
        } catch (IOException ioe) {
            return false;
        }
    }

    public static void reloadPort() {
        try {
            PORT = Integer.parseInt(findPort());
        } catch (NumberFormatException nfe) {
            Logger.getLogger(Tomcat.class.getName()).log(Level.SEVERE, null, nfe);
        }
    }

    /**
     * 获取Tomcat的端口
     * @return
     */
    private static String findPort() {
        String port = null;
        try {
            File file = new File("./tomcat/conf/server.xml");
//        	File file = new File("server.xml");
            Document doc = XmlUtils.getDocument(file);
            Element root = doc.getDocumentElement();
            NodeList nodeList = root.getElementsByTagName("Connector");
            int nodeLen = nodeList.getLength();
            for (int i = 0; i < nodeLen; i++) {
                Element node = (Element) nodeList.item(i);
                if (node.getAttribute("protocol") != null &&
                        node.getAttribute("protocol").toUpperCase().startsWith("HTTP/") &&
                        !"https".equalsIgnoreCase(node.getAttribute("scheme"))) {
                    port = node.getAttribute("port");
                    break;
                }
            }
        } catch (Exception e) {
            Logger.getLogger(Tomcat.class.getName()).log(Level.SEVERE, null, e);
            return null;
        }
        return port;
    }

    /**
     * 编辑tomcat的端口。
     * @param port
     * @return
     */
    public static boolean editPort(String port) {
        try {
            File file = new File("./tomcat/conf/server.xml");
            Document doc = XmlUtils.getDocument(file);
            Element root = doc.getDocumentElement();
            NodeList nodeList = root.getElementsByTagName("Connector");
            int nodeLen = nodeList.getLength();
            for (int i = 0; i < nodeLen; i++) {
                Element node = (Element) nodeList.item(i);
                if (node.getAttribute("protocol") != null &&
                        node.getAttribute("protocol").toUpperCase().startsWith("HTTP/")) {
                    node.setAttribute("port", String.valueOf(port));
                    break;
                }
            }
            XmlUtils.saveDocument(doc, file);
        } catch (Exception e) {
            Logger.getLogger(Tomcat.class.getName()).log(Level.SEVERE, null, e);
            return false;
        }
        return true;
    }

    public static int getPort() {
        if (PORT == -1) {
            try {
                PORT = Integer.parseInt(findPort());
            } catch (NumberFormatException nfe) {
                Logger.getLogger(Tomcat.class.getName()).log(Level.SEVERE, null, nfe);
            }
        }
        return PORT;
    }
}
